package com.demo2do.fms.admin.dao.vehicle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.fms.admin.entity.search.supplier.AbnormalCapacitySearch;
import com.demo2do.fms.entity.supplier.common.Supplier;
import com.demo2do.fms.entity.vehicle.source.LockStatus;
import com.demo2do.fms.entity.vehicle.source.VehicleSourceLockRecord;

/**
 * 
 * @author dan
 *
 */
@Repository("vehicleLockRecordDaoSupport")
public class VehicleLockRecordDaoSupport {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /**
     * 
     * @param status
     * @return
     */
    @SuppressWarnings("unchecked")
    public int countVehicleLockRecord(LockStatus status) {
        
        String sentence = "FROM VehicleSourceLockRecord WHERE status = :status AND vehicleSource.disabled = false";
        List<VehicleSourceLockRecord> records = genericDaoSupport.searchForList(sentence, "status", status);
        
        List<VehicleSourceLockRecord> result = this.getDistinctVehicleRecord(records);
        return result.size();
        
    }
    
    /**
     * 
     * @param supplier
     * @param status
     * @return
     */
    @SuppressWarnings("unchecked")
    public int countVehicleLockRecord(Supplier supplier, LockStatus status) {
    	
        String sentence = "FROM VehicleSourceLockRecord WHERE vehicleSource.supplier = :supplier AND status = :status AND vehicleSource.disabled = false ORDER BY createTime DESC";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("supplier", supplier);
        parameters.put("status", status);
        List<VehicleSourceLockRecord> records = genericDaoSupport.searchForList(sentence);
        
        List<VehicleSourceLockRecord> result = this.getDistinctVehicleRecord(records);
        return result.size();
        
    }
    
    /**
     * 
     * @param status
     * @param page
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<VehicleSourceLockRecord> listVehicleLockRecord(LockStatus status, Page page) {
    	
        String sentence = "FROM VehicleSourceLockRecord WHERE status = :status AND vehicleSource.disabled = false ORDER BY createTime DESC";
        List<VehicleSourceLockRecord> records = genericDaoSupport.searchForList(sentence, "status", status, page.getBeginIndex(), page.getEveryPage());
        
        return this.getDistinctVehicleRecord(records);
        
    }
    
    /**
     * 
     * @param supplier
     * @param status
     * @param page
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<VehicleSourceLockRecord> listVehicleLockRecord(Supplier supplier, LockStatus status, Page page) {
    	
        String sentence = "FROM VehicleSourceLockRecord WHERE vehicleSource.supplier = :supplier AND status = :status AND vehicleSource.disabled = false ORDER BY createTime DESC";
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("supplier", supplier);
        parameters.put("status", status);
        List<VehicleSourceLockRecord> records = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        
        return this.getDistinctVehicleRecord(records);
        
    }
    
    /**
     * 
     * @param records
     * @return
     */
    private List<VehicleSourceLockRecord> getDistinctVehicleRecord(List<VehicleSourceLockRecord> records) {
    	
        List<VehicleSourceLockRecord> result = new ArrayList<VehicleSourceLockRecord>();
        List<Long> vehicleSourceIds = new ArrayList<Long>();
        
        for(VehicleSourceLockRecord vehicleSourceLockRecord : records) {
            if(!vehicleSourceIds.contains(vehicleSourceLockRecord.getVehicleSource().getId())) {
                vehicleSourceIds.add(vehicleSourceLockRecord.getVehicleSource().getId());
                result.add(vehicleSourceLockRecord);
            }
        }
        
        return result;
        
    }
    
    /**
     * @param search
     * @return
     */
    @SuppressWarnings("unchecked")
    public int countVehicleLockRecord(AbnormalCapacitySearch search) {
        
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        List<VehicleSourceLockRecord> records = genericDaoSupport.searchForList(sentence, parameters);
        
        List<VehicleSourceLockRecord> result = this.getDistinctVehicleRecord(records);
        return result.size();
        
    }
    
    /**
     * @param search
     * @param page
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<VehicleSourceLockRecord> search(AbnormalCapacitySearch search, Page page) {
        
        String sentence = search.buildSentenceTemplate();
        Map<String, Object> parameters = search.buildParameters();
        List<VehicleSourceLockRecord> records = genericDaoSupport.searchForList(sentence, parameters, page.getBeginIndex(), page.getEveryPage());
        
        List<VehicleSourceLockRecord> result = this.getDistinctVehicleRecord(records);
        return result;
        
    }

}
